<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :immediate-check="false"
    >
      <van-cell v-for="data in datalist" :key="data.filmId" @click="handleChangePage(data.filmId)" v-chuxian>
        <!-- <router-link to="/detail">{{ data }}</router-link> -->
         <img :src="data.poster" alt="">
        <div>
          <div class="title">{{ data.name }}</div>
          <div class="content">
            <div>观众评分：<span style="color:red;">{{ data.grade?data.grade:'暂无评分' }}</span></div>
            <div class="actors">主演：{{ data.actors | actorsFilter }}</div>
            <div>
              {{ data.nation }} | {{ data.runtime }}分钟
            </div>
          </div>
        </div>
      </van-cell>
    </van-list>
  </div>
</template>

<script>
// import axios from 'axios'
import http from '@/util/http'
import Vue from 'vue'

Vue.filter('actorsFilter', (data) => {
  if (data === undefined) return '暂无主演'
  return data.map(item => item.name).join('  ')
})

export default {
  data () {
    return {
      datalist: [],
      loading: false,
      finished: false,
      current: 1,
      total: 0
    }
  },

  mounted () {
    http({
      url: `/gateway?cityId=${this.$store.state.cityId}&pageNum=1&pageSize=10&type=1&k=2024520`,
      headers: {
        'X-Host': 'mall.film-ticket.film.list'
      }
    }).then(res => {
      // console.log(res.data.data)
      this.datalist = res.data.data.films
      this.total = res.data.data.total
    })
  },

  methods: {
    onLoad () {
      // console.log('this.total',this.total)
      // console.log('this.datalist.length',this.datalist.length)
      // console.log('this.datalist',this.datalist)
      // console.log('到底了')
      if (this.datalist.length === this.total && this.total !== 0) {
        this.finished = true
        return
      }
      // console.log('this.total',this.total)
      // console.log('this.datalist.length',this.datalist.length)
      // console.log('this.datalist',this.datalist)

      // 如果刚进入页面还没有进行懒加载时，就跳到别的页面并出现了滚动条，然后再返回到本页面，
      // datalist长度为0时，current就会增加到2
      // 所以此处需要控制一下current的值。就是当datalist长度为10时，current为1
      if (this.datalist.length === 10) {
        this.current = 1
      }
      this.current++

      // console.log('this.current', this.current)
      http({
        url: `/gateway?cityId=310100&pageNum=${this.current}&pageSize=10&type=1&k=2024520`,
        headers: {
          'X-Host': 'mall.film-ticket.film.list'
        }
      }).then(res => {
        // console.log(res.data.data.films)
        this.datalist = [...this.datalist, ...res.data.data.films]
        this.loading = false
      })
    },

    handleChangePage (id) {
      // 编程式导航
      // location.href = '#/detail'
      // 1.通过路径跳转
      // this.$router.push(`/detail/${id}`)

      // 2.通过命名路由跳转
      // console.log(id)
      this.$router.push({
        name: 'kerwinDetail',
        params: {
          id
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.van-list {
  .van-cell {
    overflow: hidden;
    padding: .8333rem;
    img {
      width: 3.6667rem;
      float: left;
    }
    .title {
      font-size: .8889rem;
    }
    .content {
      font-size: .7222rem;
      color: gray;
      .actors {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        width: 11.1111rem;
      }
    }
  }
}
</style>
